package com.baidu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.constant.MessageConstant;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.entity.Result;
import com.baidu.pojo.Member;
import com.baidu.service.MemberService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/member")
public class MemberController {
    @Reference
    MemberService memberService;

    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return memberService.findByPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),queryPageBean.getQueryString());
    }

    @RequestMapping("/findMemberById")
    public Result findMemberById(Integer id){
        try{
            Member member=memberService.findMemberById(id);
            return new Result(true,null,member);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,"查询信息失败");
        }
    }

    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member){
        try{
            memberService.addMember(member);
            return new Result(true, MessageConstant.ADD_MEMBER_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MEMBER_FAIL);
        }
    }

    @RequestMapping("/editMember")
    public Result editMember(@RequestBody Member member){
        try{
            memberService.editMember(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MEMBER_FAIL);
        }
    }

    @RequestMapping("/deleteMember")
    public Result editMember(Integer id){
        try{
            memberService.deleteMemberById(id);
            return new Result(true, MessageConstant.DELETE_MEMBER_SUCCESS);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MEMBER_FAIL);
        }
    }


}
